$(function () {
    //checkLoginStatus();
    //getProducts();
    checkUrl();
    orderBySalesEvent();
    Comprehensive();
    PriceMaxOrMinSorting();
    addShopCar();
    Jump();


    function checkUrl() {
        var url = window.location.href;
        var urlText = Params(url);
        if (!urlText.searchInfo) {
            getProducts();
        } else {
            searchInfo(urlText);
        }
    }


    function searchInfo(urlText) {
        var option = {
            url: HOST + '/admin/proList.php',
            data: {
                getType: 'search',
                searchText: urlText.searchInfo
            }
        };
        request(option).then(function (res) {
            createProListHtml(res);
            $('#loading').removeClass('show');
        })
    }


    function addShopCar() {
        $('#proList').on('click', '.add', function () {
            var option = {
                url: HOST + '/admin/prolist.php',
                type: 'post',
                data: {
                    postType: 'addOne',
                    id: $(this).attr('data-id'),
                    user: LsyCookie.getItem('loginUserName')
                }
            };
            request(option).then(function (res) {
                if (res.status === 3) {
                    LsyCookie.removeItem('loginUserName', '../');
                    window.location.href = 'login.html';
                }
                alert(res.msg);
                //window.location.href = '../html/shopCar.html';
            })
        })
    }

    function orderBySalesEvent() {
        $('#orderBySales').tap(function () {
            var option = {
                url: HOST + '/admin/prolist.php',
                data: {
                    getType: 'orderBySales',
                    num: 8
                }
            };
            $('#loading').addClass('show');
            request(option).then(function (res) {
                createProListHtml(res);
                $('#loading').removeClass('show');
            })
        })
    }

    function PriceMaxOrMinSorting() {
        $('#min').blur(function () {
            requestToo()
        });
        $('#max').blur(function () {
            requestToo();
        })

    }

    function requestToo() {
        var min = $('#min').val();
        var max = $('#max').val();
        min = min ? min : 0;
        max = max ? max : 999999;
        var option = {
            url: HOST + '/admin/prolist.php',
            data: {
                getType: 'max',
                num: 6,
                max: max,
                min: min
            }
        };
        $('#loading').addClass('show');
        request(option).then(function (res) {
            createProListHtml(res);
            $('#loading').removeClass('show');
        })
    }

    function Comprehensive() {
        $('#Sorting').change(function () {
            var _vm = $(this);
            var index = _vm.val() * 1;
            var orderType = '';
            switch (index) {
                case 1:
                    orderType = 'Comprehensive';
                    break;
                case 2:
                    orderType = 'PriceSorting';
                    break;
                case 3:
                    orderType = 'orderBySale';
                    break;
                case 4:
                    orderType = 'styleBySales';
                    break;
                default:
                    orderType = 'Comprehensive';
                    break;
            }
            var option = {
                url: HOST + '/admin/proList.php',
                data: {
                    getType: orderType,
                    num: 5
                }
            };
            $('#loading').addClass('show');
            request(option).then(function (res) {
                createProListHtml(res);
                $('#loading').removeClass('show');
            })
        })
    }

    function getProducts() {
        var option = {
            url: HOST + '/admin/prolist.php',
            data: {
                getType: 'default',
                num: 8
            }
        };
        $('#loading').addClass('show');
        request(option).then(function (res) {
            createProListHtml(res);
            $('#loading').removeClass('show');
        })
    }

    function createProListHtml(data) {
        var str = '';
        data.forEach(function (item) {
            str += '<div class="item" data-id="' + item.id + '">'
                + '<img src="' + item.img_src + '" alt="pic" class="link">'
                + '<h3>' + item.title + '</h3>'
                + '<div class="operation">'
                + '<div class="price">'
                + '<div class="current">￥' + item.price + '</div>'
                + '<div class="old">￥' + item.old_price + '</div>'
                + '</div>'
                + '<div class="buy">'
                + '<div class="add" data-id="' + item.id + '">加入购物车</div>'
                + '<div class="pay">立即支付</div>'
                + '</div>'
                + '</div>'
                + '</div>';
        });
        $('#proList').html(str);
    }

    function Jump() {
        $('#proList').on('click', '.link', function () {
            var id = parseInt($(this).parents('.item').attr('data-id'));
            window.location.href = 'pro_details.html?id=' + id + '';
        });
        $('#proList').on('click', '.pay', function () {
            window.location.href = 'confirm_order.html'
        });
    }

    function Params() {
        var result = {}, hash = location.hash, search = location.search;
        result.docName = decodeURIComponent(location.pathname).match(/\/([^\/]+)\.html$/)[1];//提取文件名
        if (hash) {
            result.hash = hash.replace(/^\#/, ''); //提前锚点
        }
        if (search) {
            var arr = search.replace(/^\?/, '').split(/\&/g); //提前所有search参数
            for (var i = 0, n = arr.length; i < n; i++) {
                var key = arr[i].split(/\=/)[0];
                result[key] = decodeURIComponent(arr[i].split(/\=/)[1]);
            }
        }
        return result;
    }

});